<template>
    <el-dialog
        :before-close="resetForm"
        :close-on-click-modal="false"
        :visible.sync="visible"
        append-to-body
        title="编辑文件发布信息"
        width="560px"
    >
        <div class="edit_page_div">
            <el-form ref="form" :model="form" :rules="rules" :show-message="false" label-width="100px">
                <el-form-item label="文件编号" prop="fileCode">
                    <el-input v-model="form.fileCode" style="width: 400px;" placeholder="系统生成" disabled></el-input>
                </el-form-item>
                <el-form-item label="文件名称" prop="fileName">
                    <el-input v-model="form.fileName" style="width: 400px;"></el-input>
                </el-form-item>
                <el-form-item label="接收企业" prop="institutionIdNames" style="margin-bottom: 20px;">
                    <el-button type="primary" @click="submit">选择企业</el-button>
                    <el-tag :key="tag" v-for="tag in form.institutionIdNames" closable :disable-transitions="false" @close="delInstitution(tag)">{{tag}}</el-tag>
                </el-form-item>
                <el-form-item label="备注" prop="remarks">
                    <el-input v-model="form.remarks" style="width: 400px;" type="textarea" rows="3"></el-input>
                </el-form-item>
                <el-form-item label="符件信息" prop="fileList">
                    <div class="el_file_list" style="width: 400px;">
                        <el-button type="text" @click="showUpload=true">上传文件</el-button>
                        <div v-for="item in form.fileList" class="show-file">
                            <div class="show_file_name" @click="$common.downloadFile(item.id)">{{ item.fileName }}</div>
                            <el-button class="el-icon-error" type="text" @click="deleteFile(item)"></el-button>
                        </div>
                    </div>
                </el-form-item>
            </el-form>
            <div class="edit_button">
                <el-button v-loading.fullscreen.lock="loading" type="primary" @click="submit">保 存</el-button>
                <el-button type="primary" @click="resetForm">取 消</el-button>
            </div>
            <upload-file :visible.sync="showUpload" suffixes="*" @change="setFileList"></upload-file>
        </div>
    </el-dialog>
</template>

<script>
import UploadFile from "@/components/upload/uploadFile.vue";
import OrganizationSelect from "@/views/common/organization/organizationSelect.vue";

export default {
    name: "newsFilesEdit",
    components: {OrganizationTree, OrganizationSelect, UploadFile},
    props: {
        visible: Boolean,
        id: String,
    },
    data() {
        return {
            loading: false,
            showUpload: false,

            form: {
                fileCode: '',
                fileName: '',
                remarks: '',
                institutionIdList: [],
                institutionIdNames: '',
                fileList: [],
                fileIdList: [],
            },
            rules: {
                fileName: [{required: true, message: '不能为空', trigger: 'change'}],
                fileList: [{required: true, message: '不能为空', trigger: 'change'}],
            },
        }
    },
    watch: {
        visible(newVal) {
            if (newVal) {
                if (this.id) {
                    this.getData();
                }
            }
        }
    },
    methods: {
        getData() {
            this.loading = true;
            this.$axios.get("/CHILD_SYSTEM/newsFiles/getOneEntity/" + this.id).then(res => {
                this.form = res.data;
            }).finally(() => {
                this.loading = false;
            })
        },
        setFileList(data) {
            if (data != null && data.length > 0) {
                data.forEach(file => {
                    this.form.fileList.push(file);
                })
            }
        },
        deleteFile(row) {
            this.form.fileList = this.form.fileList.filter(item => item.id !== row.id);
        },
        delInstitution (tag) {
            this.form.institutionIdNames.splice(this.form.institutionIdNames.indexOf(tag), 1);
        },
        submit() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    this.form.fileIdList = [];
                    this.form.fileList.forEach(file => {
                        this.form.fileIdList.push(file.id);
                    })
                    if (this.form.fileIdList.length == 0) {
                        this.$message.error("请上传文件！");
                    }
                    this.loading = true;
                    this.$axios.post("/CHILD_SYSTEM/newsFiles/" + (this.id ? "update" : "insert"), this.form).then(() => {
                        this.resetForm();
                        this.$message.success(res.message);
                    }).finally(() => {
                        this.loading = false;
                    })
                } else {
                    this.$message.error('表单填写有误');
                    return false;
                }
            });
        },
        resetForm() {
            this.$refs.form.resetFields();
            this.form = this.$options.data().form;
            this.$emit('update:visible', false);
            this.$emit('refreshList');
        },
    }
}
</script>

<style scoped>

</style>